/**
    简单的例子 实现  揭露 async await的原理 太牛逼了
 */

function async(generator) {
  let iterator = generator();
  function handle(iteratorResult) {
    if (iteratorResult.done) return;
    const iteratorValue = iteratorResult.value;
    if (iteratorValue instanceof Promise) {
      iteratorValue
        .then((res) => handle(iterator.next(res)))
        .catch((err) => iterator.throw(err));
    }
  }
  try {
    handle(iterator.next());
  } catch (error) {
    iterator.throw(error);
  }
}

async(function* () {
  try {
    let res1 = yield axios.get('http://localhost:666/base');
    console.log('res1', res1);
    let res2 = yield axios.get(
      'http://localhost:666/about?user=' + res1.data.name
    );
    console.log('res2', res2);
    let res3 = yield axios.get('http://localhost:666/detail?msg=' + res2.data);
    console.log('res3', res3);
  } catch (error) {
    console.log('has some error');
  }
});
